package com.pig4cloud.pig.module.service.gate.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.exception.CustomException;
import com.pig4cloud.pig.module.api.entity.estate.Estate;
import com.pig4cloud.pig.module.api.entity.excel.GateCarList;
import com.pig4cloud.pig.module.api.entity.gate.BarrierGate;
import com.pig4cloud.pig.module.api.entity.gate.BarrierGateCarList;
import com.pig4cloud.pig.module.mapper.estate.EstateMapper;
import com.pig4cloud.pig.module.mapper.gate.BarrierGateCarListMapper;
import com.pig4cloud.pig.module.mapper.gate.BarrierGateMapper;
import com.pig4cloud.pig.module.service.gate.IBarrierGateCarListService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 道闸的 归属  黑白名单 Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-20
 */
@Service
public class BarrierGateCarListServiceImpl extends ServiceImpl<BarrierGateCarListMapper, BarrierGateCarList> implements IBarrierGateCarListService {

    @Resource
    private BarrierGateMapper barrierGateMapper;
    @Resource
    private EstateMapper estateMapper;
    @Resource
    private BarrierGateCarListMapper barrierGateCarListMapper;
    @Override
    public String importCarData(List<GateCarList> templateList, Boolean isUpdateSupport, String operName) {
        if (Validator.isNull(templateList) || templateList.size() == 0) {
            throw new CustomException("导入模板数据不能为空！");
        }
        int ret = 0;
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GateCarList template : templateList) {
            if (template.getGateName()!=null&&template.getGateName()!=""&&template.getEstateName()!=null&&template.getEstateName()!=""&&template.getCarNum()!=null&&template.getCarNum()!=""){
                try {
                    BarrierGateCarList barrierGateCarList = new BarrierGateCarList();
//               QueryWrapper<BarrierGate> qw = new QueryWrapper<>();
//               if (template.getGateName()!=null||template.getGateName()!=""){
//                   qw.eq("gate_name",template.getGateName());
//                   qw.last("limit 1");
//               }
//               BarrierGate barrierGate = barrierGateMapper.selectOne(qw);
//               if (barrierGate!=null){
//                   barrierGateCarList.setGateId(Long.valueOf(barrierGate.getId()));
//                   barrierGateCarList.setGateName(template.getGateName());
//               }
//
//                QueryWrapper<Estate> qw1 = new QueryWrapper<>();
//                if (template.getEstateName()!=null||template.getEstateName()!=""){
//                    qw.eq("estate_name",template.getEstateName());
//                    qw.last("limit 1");
//                }
//               Estate estate= estateMapper.selectOne(qw1);
//                if (estate!=null){
//                    barrierGateCarList.setEstateId(Long.valueOf(estate.getId()));
//                    barrierGateCarList.setEstateName(template.getEstateName());
//                }
                    if (template.getGateName()!=null||template.getGateName()!=""){
                        barrierGateCarList.setGateName(template.getGateName());
                        BarrierGate barrierGate = barrierGateMapper.selectOneByColumn("gate_name",template.getGateName());
                        if (barrierGate!=null){
                           barrierGateCarList.setGateId(Long.valueOf(barrierGate.getId()));
                        }
                    }
                    if (template.getEstateName()!=null||template.getEstateName()!=""){
                        barrierGateCarList.setEstateName(template.getEstateName());
                        Estate estate= estateMapper.selectOneByColumn("estate_name",template.getEstateName());
                        if (estate!=null){
                            barrierGateCarList.setEstateId(Long.valueOf(estate.getId()));
                        }
                    }
                    if (template.getCarNum()!=null||template.getCarNum()!=""){
                        barrierGateCarList.setCarNum(template.getCarNum());
                    }
                    if (template.getRealName()!=null||template.getRealName()!=""){
                        barrierGateCarList.setRealName(template.getRealName());
                    }
                    if (template.getPhoneNum()!=null||template.getPhoneNum()!=""){
                        barrierGateCarList.setPhoneNum(template.getPhoneNum());
                    }

                    if (template.getIsWhites()!=null||template.getIsWhites()!=""){
                        if (template.getIsWhites().equals("白名单")){
                            barrierGateCarList.setIsWhite(1L);
                        };
                        if (template.getIsWhites().equals("黑名单")){
                            barrierGateCarList.setIsWhite(2L);
                        };
                    }
                    if (template.getIsVips()!=null||template.getIsVips()!=""){
                        if (template.getIsVips().equals("是")){
                            barrierGateCarList.setIsVip(1L);
                        };
                        if (template.getIsVips().equals("否")){
                            barrierGateCarList.setIsVip(0L);
                        };
                    }
                    if (template.getStates()!=null||template.getStates()!=""){
                        if (template.getStates().equals("不可用")){
                            barrierGateCarList.setState(0L);
                        }
                        if (template.getStates().equals("可用")){
                            barrierGateCarList.setState(1L);
                        }
                    }
                    barrierGateCarListMapper.insert(barrierGateCarList);
                    successNum ++;
                } catch (Exception e) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、账号 " + template.getRealName() + "-" + template.getPhoneNum() + " 导入失败：";
                    failureMsg.append(msg + e.getMessage());
                    log.error(msg, e);
                }
            }
            }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
